﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using System;

public class UnitEditor : Image, IDragHandler, IEndDragHandler, IPointerDownHandler, IPointerClickHandler
{
    public EditorManager EMgr;
    public UnitBehaviour UB;

    public virtual Unit Unit
    {
        get { return UB.Unit; }
        set { UB.Unit = value; }
    }

    bool dragged = false;
    public void OnPointerDown(PointerEventData eventData)
    {
        dragged = false;
        Selected = true;
    }

    public virtual void OnDrag(PointerEventData eventData)
    {
        dragged = true;
        EMgr.CloseOpMenu();
        var offset = eventData.delta;
        transform.localPosition += new Vector3(offset.x, offset.y, 0);
        Unit.Pos = EMgr.View2World(transform.position);

        if (Unit.Src != null)
            Unit.Src.UnlinkChild(Unit);

        var src = EMgr.G.FindNearestConnectableSrc(Unit, (u) =>
        {
            EMgr.GetEditor(u).Accent = false;
            return true;
        });

        Accent = src != null;
        if (src != null)
            EMgr.GetEditor(src).Accent = true;

        if (Unit.Src != null)
            Unit.Update(0, Unit.Src.GetLinkType(Unit));
        else
            Unit.UpdateAllChildren(0);
    }

    public virtual void OnEndDrag(PointerEventData eventData)
    {
        TryLink();
    }

    public void TryLink()
    {
        var src = EMgr.G.FindNearestConnectableSrc(Unit, (u) =>
        {
            EMgr.GetEditor(u).Accent = false;
            return true;
        });

        if (src != null)
        {
            Accent = true;
            EMgr.G.SetConnection(src, Unit);
            transform.position = EMgr.World2View(Unit.Pos);
        }
    }

    public virtual void Refresh()
    {
        UB.Refresh();
        foreach (var dst in Unit.AllChildren)
            EMgr.GetEditor(dst).Refresh();
    }

    protected virtual void Update()
    {
        transform.position = EMgr.World2View(Unit.Pos);
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (dragged)
            return;

        OnClick(eventData.position);
    }

    protected virtual void OnClick(Vector2 pt)
    {
    }

    public virtual bool WellConnectable
    {
        get
        {
            return wellConnectable;
        }
        set
        {
            wellConnectable = value;
            RefreshEffect();
        }
    } bool wellConnectable = false;

    public virtual bool Accent
    {
        get { return accent; }
        set
        {
            accent = value;
            RefreshEffect();
        }
    } bool accent = false;

    public virtual bool Selected
    {
        get
        {
            return selected;
        }
        set
        {
            selected = value;
            if (EMgr.CurrentSelected != this)
                EMgr.CurrentSelected = this;

            RefreshEffect();
        }
    } protected bool selected = false;

    void RefreshEffect()
    {
        var a = Accent || selected ? 0.25f : 0;
        color = Accent || wellConnectable ? new Color(0, 0.25f, 0, a) : new Color(0.25f, 0, 0, a);
    }
}
